﻿<?php
include("../../includes/db_connect.php");
include("../../includes/safefunctions.php");
include("../pageList.php");



if(isset($_POST["submit"])) {
	if(!isset($_POST["ishidden"]))
		$_POST["ishidden"] = 0;
	else if($_POST["ishidden"] === "true")
		$_POST["ishidden"] = 1;
	if(!isset($_POST["allowScript"]))
		$_POST["allowScript"] = 0;
	else if($_POST["allowScript"] === "true")
		$_POST["allowScript"] = 1;
	if(!isset($_POST["isLink"]))
		$_POST["isLink"] = 0;
	else if($_POST["isLink"] === "true")
		$_POST["isLink"] = 1;
	
	echo "<div id=\"result\">";
	$return = mysqli_query($db, "INSERT INTO pages (name, tagName, parentId, hidden, script, text, file, allowScript, isLink) VALUES('{$db->real_escape_string($_POST["name"])}','{$db->real_escape_string($_POST["tagname"])}','{$db->real_escape_string($_POST["parentid"])}','{$db->real_escape_string($_POST["ishidden"])}','{$db->real_escape_string($_POST["pageScript"])}','{$db->real_escape_string($_POST["text"])}', NULL,'{$db->real_escape_string($_POST["allowScript"])}','{$db->real_escape_string($_POST["isLink"])}')");
	if($return) {
		echo "La till en ny sida.";
	}else{
		echo "Sidan kunde inte läggas till.";
	}
	echo "</div>";
}else{
	echo "<div id=\"result\"></div>";
	echo "<table id=\"pageList\">";
	createPageList($db);
	echo "</table>";
	echo "<form class=\"addForm\" action=\"\" method=\"POST\">
	<table>
	<tr>
	<td>NAMN:
	</td><td><input type=\"text\" name=\"name\" maxlength=\"50\"></td>
		</tr>
	<tr>
	<td>TAG-NAMN:
	</td><td><input type=\"text\" name=\"tagname\" maxlength=\"50\"></td>
		</tr>
	<tr>
	<td>INNEHÅLL:
	</td><td><textarea name=\"text\" maxlength=\"65535\"></textarea></td>
		</tr>
	<tr>
	<td>SCRIPT:
	</td><td><textarea name=\"pageScript\" maxlength=\"65535\"></textarea></td>
		</tr>
	<tr>
	<td>TILLÅT SCRIPT:
	</td><td><input type=\"checkbox\" name=\"allowScript\"></td>
		</tr>
	<tr>
	<td>ÄR EN LÄNK:
	</td><td><input type=\"checkbox\" name=\"isLink\" checked></td>
		</tr>
	<td>DOLD:
	</td><td><input type=\"checkbox\" name=\"ishidden\"></td>
		</tr>
	<td><input type=\"submit\"name=\"submit\" value=\"Lägg till\"></td>
	</table>
	</form>";
	echo "<div id=\"result\"></div>";
	echo "<div id=\"userInfo\"></div>";
}
?>

<script src="../script/jquery-1.11.1.min.js"></script>
<script src="../script/ajaxlinks.js"></script>
<script>
var _SESSION = {};
_SESSION["currentPage"] = "addPage.php";

var id = 0;
var lastClicked = null;
$('.pageList').click(function() { 
	id = this.id.substring(2);
	this.style.backgroundColor = "lightgrey";
	if(lastClicked != null)
		lastClicked.style.backgroundColor = "white";
	lastClicked = this;
});
		
$('.pageList').hover(function(){
	$('.pageList').css("cursor", "pointer");	
});

$('form.addForm').submit(function (e) {
	e.preventDefault();
	var form = document.forms[0];
	var url = "submit=true";
	url += "&name="+encodeURIComponent(form["name"].value);
	url += "&tagname="+encodeURIComponent(form["tagname"].value);
	url += "&parentid="+id;
	url += "&text="+encodeURIComponent(form["text"].value);
	url += "&pageScript="+encodeURIComponent(form["pageScript"].value);
	url += "&allowScript="+encodeURIComponent(form["allowScript"].checked);
	url += "&isLink="+encodeURIComponent(form["isLink"].checked);
	url += "&ishidden="+encodeURIComponent(form["ishidden"].checked);
	
	var page = getPage(_SESSION["currentPage"], "POST", url);
	var tmpDiv = document.createElement("div");
	tmpDiv.innerHTML = page;
	document.getElementById("result").innerHTML = tmpDiv.querySelector("[id=result]").innerHTML;
	
	return false;

});
</script>